<template>
	<view class="container">
		<!-- 状态切换 -->
		<view class="status-bar">
			<scroll-view scroll-x class="status-scroll">
				<view 
					class="status-item" 
					v-for="(item, index) in statusList" 
					:key="index"
					:class="{ active: currentStatus === item.id }"
					@click="switchStatus(item.id)"
				>
					<text class="status-name">{{item.name}}</text>
				</view>
			</scroll-view>
		</view>
		
		<!-- 售后列表 -->
		<scroll-view 
			scroll-y 
			class="after-sales-list"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="after-sales-item" v-for="(item, index) in afterSalesList" :key="index">
				<!-- 商品信息 -->
				<view class="product-info">
					<image :src="item.product.image" mode="aspectFill" class="product-img"></image>
					<view class="product-detail">
						<text class="product-name">{{item.product.name}}</text>
						<text class="product-spec">{{item.product.spec}}</text>
					</view>
				</view>
				
				<!-- 售后信息 -->
				<view class="after-sales-info">
					<view class="info-row">
						<text class="label">售后类型：</text>
						<text class="value">{{item.type}}</text>
					</view>
					<view class="info-row">
						<text class="label">申请时间：</text>
						<text class="value">{{item.applyTime}}</text>
					</view>
					<view class="info-row">
						<text class="label">售后状态：</text>
						<text class="value" :class="'status-' + item.status">{{item.statusText}}</text>
					</view>
				</view>
				
				<!-- 操作按钮 -->
				<view class="action-buttons">
					<button 
						class="action-btn" 
						v-if="item.status === 'processing'"
						@click="cancelAfterSales(item.id)"
					>取消申请</button>
					<button 
						class="action-btn primary" 
						v-if="item.status === 'processing'"
						@click="viewDetail(item.id)"
					>查看详情</button>
					<button 
						class="action-btn primary" 
						v-if="item.status === 'completed'"
						@click="viewDetail(item.id)"
					>查看详情</button>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="afterSalesList.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无售后记录</text>
			</view>
		</scroll-view>
		
		<!-- 底部按钮 -->
		<view class="footer">
			<button class="apply-btn" @click="applyAfterSales">申请售后</button>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 状态列表
const statusList = ref([
	{ id: 'all', name: '全部' },
	{ id: 'processing', name: '处理中' },
	{ id: 'completed', name: '已完成' },
	{ id: 'cancelled', name: '已取消' }
])

// 当前选中的状态
const currentStatus = ref('all')

// 售后列表数据
const afterSalesList = ref([
	{
		id: 1,
		product: {
			name: '重庆老火锅底料',
			spec: '500g/袋',
			image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
		},
		type: '退货退款',
		applyTime: '2024-03-20 14:30',
		status: 'processing',
		statusText: '处理中'
	},
	{
		id: 2,
		product: {
			name: '特色花椒油',
			spec: '200ml/瓶',
			image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
		},
		type: '换货',
		applyTime: '2024-03-19 16:20',
		status: 'completed',
		statusText: '已完成'
	}
])

// 是否还有更多数据
const hasMore = ref(true)

// 是否正在刷新
const isRefreshing = ref(false)

// 切换状态
const switchStatus = (status) => {
	currentStatus.value = status
	// 重置列表并重新加载
	afterSalesList.value = []
	hasMore.value = true
	loadMore()
}

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多数据的接口调用
	setTimeout(() => {
		hasMore.value = false
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	// 重置列表并重新加载
	afterSalesList.value = []
	hasMore.value = true
	
	setTimeout(() => {
		isRefreshing.value = false
		loadMore()
	}, 1000)
}

// 取消售后申请
const cancelAfterSales = (id) => {
	uni.showModal({
		title: '提示',
		content: '确定要取消该售后申请吗？',
		success: (res) => {
			if (res.confirm) {
				uni.showLoading({
					title: '取消中...'
				})
				
				// 这里可以添加取消售后申请的接口调用
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						title: '取消成功',
						icon: 'success'
					})
					
					// 更新列表
					afterSalesList.value = afterSalesList.value.filter(item => item.id !== id)
				}, 1000)
			}
		}
	})
}

// 查看详情
const viewDetail = (id) => {
	uni.navigateTo({
		url: `/pages/after-sales/detail?id=${id}`
	})
}

// 申请售后
const applyAfterSales = () => {
	uni.navigateTo({
		url: '/pages/after-sales/index'
	})
}

// 生命周期钩子
onMounted(() => {
	loadMore()
})
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
	padding-bottom: 120rpx;
}

/* 状态栏样式 */
.status-bar {
	background-color: #FFFFFF;
	padding: 20rpx 0;
	position: sticky;
	top: 0;
	z-index: 1;
}

.status-scroll {
	white-space: nowrap;
}

.status-item {
	display: inline-block;
	padding: 0 30rpx;
	position: relative;
}

.status-name {
	font-size: 28rpx;
	color: #666;
}

.status-item.active .status-name {
	color: #D02B29;
	font-weight: bold;
}

.status-item.active::after {
	content: '';
	position: absolute;
	bottom: -10rpx;
	left: 50%;
	transform: translateX(-50%);
	width: 40rpx;
	height: 4rpx;
	background-color: #D02B29;
	border-radius: 2rpx;
}

/* 售后列表样式 */
.after-sales-list {
	height: calc(100vh - 200rpx);
}

.after-sales-item {
	background-color: #FFFFFF;
	margin-bottom: 20rpx;
	padding: 30rpx;
}

.product-info {
	display: flex;
	margin-bottom: 20rpx;
}

.product-img {
	width: 160rpx;
	height: 160rpx;
	border-radius: 10rpx;
	margin-right: 20rpx;
}

.product-detail {
	flex: 1;
}

.product-name {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

.product-spec {
	font-size: 24rpx;
	color: #999;
}

.after-sales-info {
	border-top: 1rpx solid #EEEEEE;
	padding-top: 20rpx;
}

.info-row {
	display: flex;
	margin-bottom: 10rpx;
}

.label {
	font-size: 26rpx;
	color: #666;
	width: 160rpx;
}

.value {
	font-size: 26rpx;
	color: #333;
	flex: 1;
}

.status-processing {
	color: #FF9900;
}

.status-completed {
	color: #07C160;
}

.status-cancelled {
	color: #999;
}

.action-buttons {
	display: flex;
	justify-content: flex-end;
	margin-top: 20rpx;
	padding-top: 20rpx;
	border-top: 1rpx solid #EEEEEE;
}

.action-btn {
	margin-left: 20rpx;
	font-size: 26rpx;
	color: #666;
	background-color: #F5F5F5;
	border-radius: 30rpx;
	padding: 10rpx 30rpx;
}

.action-btn.primary {
	color: #D02B29;
	background-color: #FFF0F0;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 24rpx;
	color: #999;
}

/* 空状态样式 */
.empty-state {
	padding: 100rpx 0;
	text-align: center;
}

.empty-img {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}

/* 底部按钮样式 */
.footer {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: #FFFFFF;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
}

.apply-btn {
	width: 100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	background-color: #D02B29;
	color: #FFFFFF;
	border-radius: 40rpx;
	font-size: 32rpx;
}
</style> 